<?php
namespace app\controller\api;

use app\BaseController;
use app\model\UserModel;
use app\Request;
use app\validate\UserValidate;

/**
 * 用户相关接口
 * @package app\controller\api
 */

class User extends BaseController
{
    // 获取用户信息
    public function getUserInfo(Request $request)
    {
        $userInfo = UserModel::field('avatar,nickname,mobile')->find($request->user_id);
        if (!$userInfo) {
            $this->error('用户不存在');
        }
        $this->success('用户信息', $userInfo);
    }

    // 修改用户信息
    public function editUserInfo(Request $request)
    {
        $data = input();
        $validate = new UserValidate();
        $result = $validate->scene('editUserInfo')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }
        $user = UserModel::find($request->user_id);
        $user->avatar = $data['avatar'];
        $user->nickname = $data['nickname'];
        $user->mobile = $data['mobile'];
        if ($user->save()) {
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }


    public function test()
    {
        $arr = [
            'color' => ['白色', '黑色', '绿色'],
            'size' => ['36', '37', '38'],
            'details' => [
                'color' => '白色',
                'child' => [
                    [
                        'size' => 36,
                        'store'  => 100
                    ],
                    [
                        'size' => 37,
                        'store'  => 98
                    ]
                ]
            ]
        ];
        $this->success('ok', $arr);
    }
}
